package com.kinyx.framework.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import com.kinyx.framework.annotation.assist.CanNotNull;
import com.kinyx.framework.annotation.assist.CanNull;
import com.kinyx.framework.annotation.assist.ReturnNull;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 集合工具类
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class KMapUtils {

	/**
	 * 转换集合
	 * @param <Source> 源泛型
	 * @param <Target> 目标泛型
	 * @param mapSource 源集合
	 * @param converter 目标集合
	 * @return 新集合
	 */
	@ReturnNull("mapSource == null")
	public static <Source, Target> Map<String, Target> convert(@CanNull final Map<String, Source> mapSource, @CanNotNull final Function<Source, Target> converter) {
		if (mapSource == null) { return null; }
		if (mapSource.isEmpty()) { return Collections.emptyMap(); }
		final Map<String, Target> mapTarget = new HashMap<>(mapSource.size());
		mapSource.forEach((key, source) -> { mapTarget.put(key, converter.apply(source)); });
		return mapTarget;
	}

}
